package org.jeecg.modules.baseData.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.baseData.entity.BeiDouZhongDuan;
import org.jeecg.modules.baseData.entity.WuLianWangKa;
import org.jeecg.modules.baseData.entity.WuLianWangKaReBeiDouZhongDuan;
import org.jeecg.modules.baseData.mapper.BeiDouZhongDuanMapper;
import org.jeecg.modules.baseData.dto.BeiDouZhongDuanDTO;
import org.jeecg.modules.baseData.service.IBeiDouZhongDuanService;
import org.jeecg.modules.baseData.service.IWuLianWangKaReBeiDouZhongDuanService;
import org.jeecg.modules.baseData.service.IWuLianWangKaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;

/**
 * @Description: 北斗终端
 * @Author: wangfupeng
 * @Date: 2021-02-02
 * @Version: V1.0
 */
@Service
public class BeiDouZhongDuanServiceImpl extends ServiceImpl<BeiDouZhongDuanMapper, BeiDouZhongDuan> implements IBeiDouZhongDuanService {

    @Autowired
    private BeiDouZhongDuanMapper beiDouZhongDuanMapper;
    @Autowired
    private IWuLianWangKaReBeiDouZhongDuanService wuLianWangKaReBeiDouZhongDuanService;
    @Autowired
    private IWuLianWangKaService wuLianWangKaService;


    @Override
    public IPage<HashMap<String, Object>> getPageList(Page<BeiDouZhongDuan> page, BeiDouZhongDuanDTO tbBeiDouZhongDuanDTO) {
        return beiDouZhongDuanMapper.getPageList(page, tbBeiDouZhongDuanDTO);
    }

    /**
     * 北斗终端绑定物联网卡
     *
     * @param terminalId
     * @param cardId
     */
    @Transactional
    @Override
    public void bindCard(String terminalId, String cardId) {
        QueryWrapper<WuLianWangKaReBeiDouZhongDuan> wuLianWangKaReBeiDouZhongDuanQueryWrapper = new QueryWrapper<>();
        wuLianWangKaReBeiDouZhongDuanQueryWrapper.lambda()
                .eq(WuLianWangKaReBeiDouZhongDuan::getBeiDouZhongDuanId, terminalId)
                .or()
                .eq(WuLianWangKaReBeiDouZhongDuan::getWuLianWangKaId, cardId);

        int count = wuLianWangKaReBeiDouZhongDuanService.count(wuLianWangKaReBeiDouZhongDuanQueryWrapper);
        if (count > 0) {
            throw new JeecgBootException("北斗终端或者物联网卡已经绑定");
        }
        WuLianWangKaReBeiDouZhongDuan wuLianWangKaReBeiDouZhongDuan = new WuLianWangKaReBeiDouZhongDuan();
        wuLianWangKaReBeiDouZhongDuan.setBeiDouZhongDuanId(terminalId);
        wuLianWangKaReBeiDouZhongDuan.setWuLianWangKaId(cardId);
        wuLianWangKaReBeiDouZhongDuan.setCreateTime(new Date());

        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        wuLianWangKaReBeiDouZhongDuan.setCreateBy(sysUser.getUsername());
        wuLianWangKaReBeiDouZhongDuanService.save(wuLianWangKaReBeiDouZhongDuan);

        WuLianWangKa wuLianWangKa = new WuLianWangKa();
        wuLianWangKa.setId(cardId);
        wuLianWangKa.setBindTime(new Date());
        wuLianWangKa.setIsBindTerminal("1");
        wuLianWangKaService.updateById(wuLianWangKa);
    }

    /**
     * 北斗终端和物联网卡的关联id
     *
     * @param wuLianWangKaReBeiDouZhongDuanId
     */
    @Transactional
    @Override
    public void cancelBindCard(String wuLianWangKaReBeiDouZhongDuanId) {
        WuLianWangKaReBeiDouZhongDuan wuLianWangKaReBeiDouZhongDuan = wuLianWangKaReBeiDouZhongDuanService.getById(wuLianWangKaReBeiDouZhongDuanId);
        WuLianWangKa wuLianWangKa = new WuLianWangKa();
        wuLianWangKa.setId(wuLianWangKaReBeiDouZhongDuan.getWuLianWangKaId());
        wuLianWangKa.setBindTime(null);
        wuLianWangKa.setIsBindTerminal("0");
        wuLianWangKaService.updateById(wuLianWangKa);
        wuLianWangKaReBeiDouZhongDuanService.removeById(wuLianWangKaReBeiDouZhongDuan.getId());
    }
}
